package com.sanpu.kxe.business.main.presenter;

import android.content.DialogInterface;
import android.text.TextUtils;

import com.sanpu.kxe.R;
import com.sanpu.kxe.business.main.bean.CertificationOldOutput;
import com.sanpu.kxe.business.main.bean.CertificationOutput;
import com.sanpu.kxe.business.main.bean.UserCenterOutput;
import com.sanpu.kxe.business.main.contract.CertificationContract;
import com.sanpu.kxe.business.main.model.MainModel;
import com.sanpu.kxe.business.main.view.MainActivity;
import com.sanpu.kxe.common.cache.CacheManage;
import com.sanpu.kxe.common.cache.CacheModel;
import com.sanpu.kxe.common.interfaces.CommonConfig;
import com.sanpu.kxe.jump.eventbus.RefreshUserEvent;
import com.sanpu.kxe.module.dialog.GlobalDialog;
import com.sanpu.kxe.utils.AppContextUtil;
import com.sanpu.kxe.utils.ToastUtil;
import com.sanpu.kxe.utils.VerifyUtil;

import org.greenrobot.eventbus.EventBus;

import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

/**
 * Description：实名认证Presenter
 * Created by 薛勇军 on 2018/7/17.
 */

public class CertificationPresenter implements CertificationContract.Presenter {

    private CertificationContract.View mView;

    private CompositeSubscription mSubscription;

    private GlobalDialog failDialog;

    private CertificationOutput mCertificationOutput;

    public CertificationPresenter(CertificationContract.View mView) {
        this.mView = mView;
        mSubscription = new CompositeSubscription();
    }

    @Override
    public void initDataView(CertificationOutput certificationOutput) {
        this.mCertificationOutput = certificationOutput;
        if (certificationOutput == null) {
            mView.showLoadingDialog(false);
            Subscription subscription = MainModel.getInstance().getAuthInfo()
                    .subscribeOn(Schedulers.io())
                    .unsubscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Observer<CertificationOldOutput>() {
                        @Override
                        public void onCompleted() {
                            mView.dismissLoadingDialog();
                        }

                        @Override
                        public void onError(Throwable e) {
                            mView.dismissLoadingDialog();
                            mView.showErrorText(e);
                        }

                        @Override
                        public void onNext(CertificationOldOutput authInfoOutput) {
                            mView.showViewData(authInfoOutput);
                        }
                    });
            mSubscription.add(subscription);
        }
    }

    @Override
    public void onCommitData(String bankCardNumber, String bankPhoneNumber) {
        if (TextUtils.isEmpty(bankCardNumber)) {
            ToastUtil.showTextViewPrompt("结算卡号不能为空");
            return;
        }
        if (TextUtils.isEmpty(bankPhoneNumber)) {
            ToastUtil.showTextViewPrompt("手机号不能为空");
            return;
        }
        if (VerifyUtil.checkPhoneNumber(bankPhoneNumber)) {
            ToastUtil.showTextViewPrompt("请输入正确的手机号");
            return;
        }
        if (mCertificationOutput == null) {
            ToastUtil.showTextViewPrompt("认证异常，请重新开始认证流程");
            return;
        }
        mCertificationOutput.setCardNo(bankCardNumber);
        mCertificationOutput.setPhoneNo(bankPhoneNumber);
        mView.showLoadingDialog(false, "正在认证中...");
        Subscription subscription = MainModel.getInstance().realnameAuthentication(mCertificationOutput)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<String>() {
                    @Override
                    public void onCompleted() {
                        mView.dismissLoadingDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissLoadingDialog();
                        mView.showErrorText(e);
                    }

                    @Override
                    public void onNext(String s) {
                        UserCenterOutput mOutput = CacheManage.getInstance().getCache(CacheModel.USER_CENTER_KEY);
                        if (mOutput != null) {
                            mOutput.setChkSts(CommonConfig.AUTHEN_PASS);
                        }
                        CacheManage.getInstance().saveCache(CacheModel.USER_CENTER_KEY, mOutput);
                        EventBus.getDefault().post(new RefreshUserEvent());
                        GlobalDialog dialog = new GlobalDialog(mView.getContext())
                                .content("恭喜你完成实名认证！")
                                .headerIcon(R.drawable.img_success_result);
                        dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                            @Override
                            public void onDismiss(DialogInterface dialog) {
                                AppContextUtil.finishAllExclByActivity(MainActivity.class.getName());
                            }
                        });
                        dialog.show();
                    }
                });
        mSubscription.add(subscription);
    }

    @Override
    public void onDestroyPresenter() {
        if (mSubscription != null && !mSubscription.isUnsubscribed()) {
            mSubscription.unsubscribe();
            mSubscription = null;
        }
        if (failDialog != null && failDialog.isShowing()) {
            failDialog.dismiss();
            failDialog = null;
        }
        if (mView != null) {
            mView = null;
        }
    }
}
